package org.kl.auditsupervision.service.fnd;

 

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.kl.auditsupervision.entity.fnd.EffectEvaluate;
import org.kl.auditsupervision.repository.fnd.EffectEvaluateDao;
import org.kl.bf.entity.BusinessEntity;
import org.kl.bf.service.BasicService;
import org.kl.bf.utils.BasicConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;
/**
 * @author Code Generator
 * @since  2016-07-25 10:53:52
 * @see 措施改进效果自评表
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class EffectEvaluateService extends BasicService<EffectEvaluate> {
	@Autowired
	private EffectEvaluateDao effectEvaluateDao;
	
	
	public List<EffectEvaluate> findByAuditProDtlIdAndStatusIsNull(String auditProDtlId){
		return effectEvaluateDao.findByAuditProDtlIdAndStatusIsNull(auditProDtlId);
	}
	
	public void save(EffectEvaluate entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		effectEvaluateDao.save(entity);
		if (StringUtils.isNotBlank(detailInserted)) {
			List<EffectEvaluate> inserts = JSON.parseArray(detailInserted, EffectEvaluate.class);
			if (Collections3.isNotEmpty(inserts)) {
				for (EffectEvaluate a : inserts) {
					// 增加与主表相关的数据复制代码
					effectEvaluateDao.save(a);
				}
			}
		}
		if (StringUtils.isNotBlank(detailUpdated)) {
			List<EffectEvaluate> updates = JSON.parseArray(detailUpdated, EffectEvaluate.class);
			if (Collections3.isNotEmpty(updates)) {
				for (EffectEvaluate b : updates) {
					effectEvaluateDao.save(b);
				}
			}
		}
		if (StringUtils.isNotBlank(detailDeleted)) {
			List<EffectEvaluate> deletes = JSON.parseArray(detailDeleted, EffectEvaluate.class);
			if (Collections3.isNotEmpty(deletes)) {
				effectEvaluateDao.delete(deletes);
			}
		}
	}
	
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0,j=0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				EffectEvaluate entity = basicDao.findOne(id);
				if(entity.getStatus()==null||!entity.getStatus().equals("1")){
					basicDao.delete(id);
					i++;
				}else{
					j++;
				}
			}
		}
		return "成功删除" + i + "条记录,忽略" + j + "条已审批记录";
	}
}
